//
//  JGLoginContentView.m
//  JGRider
//
//  Created by 单启志 on 2018/3/2.
//  Copyright © 2018年 ffq. All rights reserved.
//

#import "JGLoginContentView.h"

@interface JGLoginContentView()
@property (weak, nonatomic) IBOutlet UITextField *accountTF;//账号
@property (weak, nonatomic) IBOutlet UITextField *pwdTF;//密码

@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *validateLoginBtn;


@end

@implementation JGLoginContentView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self=[super initWithFrame:frame]) {
        self=[[[NSBundle mainBundle]loadNibNamed:NSStringFromClass([self class]) owner:nil options:nil]lastObject];
        self.frame=frame;
        [self layoutSubViews];
    }
    return self;
}

-(void)awakeFromNib{
    [super awakeFromNib];
}

-(void)layoutSubViews{
    self.loginBtn.layer.cornerRadius=AdaptedWidth(3);
    self.loginBtn.layer.masksToBounds=YES;
    self.validateLoginBtn.layer.cornerRadius=AdaptedWidth(3);
    self.validateLoginBtn.layer.masksToBounds=YES;
}

#pragma mark - event
//登录
- (IBAction)onLoginBtnClick:(id)sender {
    if ([self validateInput]==NO) {
        return;
    }
    if ([self.delegate respondsToSelector:@selector(contentViewLoginEventWithParams:)]) {
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        dic[@"roleId"]=roleId;
        dic[@"account"]=[self.accountTF.text stringByTrim];
        dic[@"password"]=[self.pwdTF.text stringByTrim];
        [self.delegate contentViewLoginEventWithParams:dic];
    }
}
//验证码登录
- (IBAction)onValidateLoginBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(contentViewValidateLoginEvent)]) {
        [self.delegate contentViewValidateLoginEvent];
    }
}
//注册
- (IBAction)onRegistBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(contentViewRegistEvent)]) {
        [self.delegate contentViewRegistEvent];
    }
}
//忘记密码
- (IBAction)onForgetPwdBtnClick:(id)sender {
    if ([self.delegate respondsToSelector:@selector(contentViewForgetPwdEvent)]) {
        [self.delegate contentViewForgetPwdEvent];
    }
}


-(void)layoutSubviews{
    [super layoutSubviews];
}

#pragma mark -  验证输入框合法性
-(BOOL)validateInput{
    if([self validateWithFT:self.accountTF withMessage:@"请输入登录账号"]==NO){
        return NO;
    }
    if([self validateWithFT:self.pwdTF withMessage:@"请输入登录密码"]==NO){
        return NO;
    }
    return YES;
}
//验证输入框非空
-(BOOL)validateWithFT:(UITextField *)tf withMessage:(NSString*)message{
    if (![tf.text.stringByTrim isEqualToString:@""]&&[tf.text isNotBlank]) {
        return YES;
    }else{
        [MBProgressHUD showError:message ToView:self];
        return NO;
    }
}
@end
